package com.schoolai.schoolstudy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.schoolai.entity.SchoolStudyEnglishWorkTask;
import com.schoolai.entity.SchoolStudyEnglishWorkTaskList;
import com.schoolai.schoolstudy.mapper.SchoolStudyEnglishWorkTaskListMapper;
import com.schoolai.schoolstudy.mapper.SchoolStudyEnglishWorkTaskMapper;
import com.schoolai.schoolstudy.req.StudyEnglishWordTaskREQ;
import com.schoolai.schoolstudy.service.SchoolStudyEnglishWorkTaskService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.schoolai.util.base.Result;
import io.micrometer.common.util.StringUtils;
import jakarta.transaction.Transactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bobby
 * @since 2025-09-29
 */
@Slf4j
@Service
public class SchoolStudyEnglishWorkTaskServiceImpl extends ServiceImpl<SchoolStudyEnglishWorkTaskMapper, SchoolStudyEnglishWorkTask> implements SchoolStudyEnglishWorkTaskService {
    @Autowired
    SchoolStudyEnglishWorkTaskListMapper schoolStudyEnglishWorkTaskListMapper;
    @Override
    public Result queryPage(StudyEnglishWordTaskREQ req) {
        // QueryWrapper<SchoolStudyEnglishWorkTask> wrapper = new QueryWrapper();
        LambdaQueryWrapper<SchoolStudyEnglishWorkTask> wrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(req.getWorkTaskName())){
            // wrapper.like("work_task_name", req.getWorkTaskName());
            wrapper.like(SchoolStudyEnglishWorkTask::getWorkTaskName, req.getWorkTaskName());
        }
        // wrapper.orderByDesc("id");
        wrapper.orderByDesc(SchoolStudyEnglishWorkTask::getId);
        return Result.ok(baseMapper.selectPage(req.getPage(), wrapper));
    }

    @Override
    public Result getTaskAndListById(String id) {
        log.info("单词长度"+baseMapper.getTaskAndListById(id));
        return Result.ok(baseMapper.getTaskAndListById(id));
    }

    @Override
    public Result getTaskAndListById(String id, Boolean isMPJ) {
        MPJLambdaWrapper<SchoolStudyEnglishWorkTask> wrapper = JoinWrappers.lambda(SchoolStudyEnglishWorkTask.class)
                .selectCollection(SchoolStudyEnglishWorkTaskList.class,SchoolStudyEnglishWorkTask::getSchoolStudyEnglishWorkTaskList)
                .leftJoin(SchoolStudyEnglishWorkTaskList.class,SchoolStudyEnglishWorkTaskList::getTaskId,SchoolStudyEnglishWorkTask::getId)
                .eq(SchoolStudyEnglishWorkTask::getId, id)
        ;
        List<SchoolStudyEnglishWorkTask> taskList = baseMapper.selectJoinList(SchoolStudyEnglishWorkTask.class,wrapper);
        return Result.ok(taskList);
    }
    @Transactional
    @Override
    public Result deleteTaskAndListById(String id) {
        List<SchoolStudyEnglishWorkTask> taskList = (List<SchoolStudyEnglishWorkTask>) this.getTaskAndListById(id,true).getData();
        List<String> ids = new ArrayList<>();
        taskList.forEach(task -> {
            task.getSchoolStudyEnglishWorkTaskList().forEach(schoolStudyEnglishWorkTask -> {
                System.out.println(schoolStudyEnglishWorkTask.getId());
                ids.add(schoolStudyEnglishWorkTask.getId());
            });
        });
        // 删除单词任务下单词
        schoolStudyEnglishWorkTaskListMapper.deleteByIds(ids);
        // 删除单词任务
        baseMapper.deleteById(id);
        return Result.ok();
    }
}




